package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.AirConditionerContract;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;

/**
 * Created by sunlg on 2017/5/27.
 */

public class AirConditionerPresenter implements AirConditionerContract.Presenter {

    private AirConditionerContract.View mView;
    private SHServiceWrapper shServiceWrapper;

    public AirConditionerPresenter() {
        shServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void startAirConditioner(int deviceId) {
        shServiceWrapper.airConditionerOn(new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.startSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void stopAirConditioner(int deviceId) {
        shServiceWrapper.airConditionerOff(new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.stopSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(AirConditionerContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
